function out = waldTestRegimeShift(theta2,AVarTheta2,nx)

% Test for change in intercepts if possible
if any(strcmp('h01_2',AVarTheta2.names))
    numTheta2 = length(AVarTheta2.names);
    pos_h0_1 = zeros(numTheta2,nx);
    pos_h0_2 = zeros(numTheta2,nx);
    for i=1:nx
        pos_h0_1(:,i) = strcmp(['h0',num2str(i),'_1'],AVarTheta2.names);
        pos_h0_2(:,i) = strcmp(['h0',num2str(i),'_2'],AVarTheta2.names);
    end
    R = (pos_h0_1-pos_h0_2)';
     if numTheta2 ~= length(fieldnames(theta2))
        theta2Test = reduceTheta2(theta2,AVarTheta2.names);
    else
        theta2Test = theta2;
    end
    theta2TestValues = struct2array(theta2Test)';
    Q_intercept = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
    pValue_intercept = chi2cdf(Q_intercept,nx,'upper');
else
    Q_intercept = NaN;
    pValue_intercept = NaN;
end

% Test for change in slopes
if any(strcmp('hx11_2',AVarTheta2.names)) || any(strcmp('hx22_2',AVarTheta2.names)) ...
    || any(strcmp('hx33_2',AVarTheta2.names)) || any(strcmp('hx44_2',AVarTheta2.names))
    numTheta2 = length(AVarTheta2.names);
    pos_hx_1 = zeros(numTheta2,nx*nx);
    pos_hx_2 = zeros(numTheta2,nx*nx);
    idx      = 0;
    for i=1:nx
        for j=1:nx
            idx = idx + 1;
            pos_hx_1(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
    end
    R = (pos_hx_1-pos_hx_2)';
    if numTheta2 ~= length(fieldnames(theta2))
        theta2Test = reduceTheta2(theta2,AVarTheta2.names);
    else
        theta2Test = theta2;
    end
    theta2TestValues = struct2array(theta2Test)';
    Q_slope = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
    pValue_slope = chi2cdf(Q_slope,nx*nx,'upper');
    
    % testing upper left block: hx_11
    pos_hx_1_11 = zeros(numTheta2,2*2);
    pos_hx_2_11 = zeros(numTheta2,2*2);
    idx      = 0;
    for i=1:2
        for j=1:2
            idx = idx + 1;
            pos_hx_1_11(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2_11(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
    end
    if sum(pos_hx_2_11(:)) == 0
        % no switching parameters
        pValue_slope_11 = NaN;
    else
        R = (pos_hx_1_11-pos_hx_2_11)';
        if numTheta2 ~= length(fieldnames(theta2))
            theta2Test = reduceTheta2(theta2,AVarTheta2.names);
        else
            theta2Test = theta2;
        end
        theta2TestValues = struct2array(theta2Test)';
        Q_slope_11 = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
        pValue_slope_11 = chi2cdf(Q_slope_11,2*2,'upper');
    end
    
    % testing upper right block: hx_12
    pos_hx_1_12 = zeros(numTheta2,2*2);
    pos_hx_2_12 = zeros(numTheta2,2*2);
    idx      = 0;
    for i=1:2
        for j=3:4
            idx = idx + 1;
            pos_hx_1_12(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2_12(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
    end
    if sum(pos_hx_2_12(:)) == 0
        % no switching parameters
        pValue_slope_12 = NaN;
    else
        R = (pos_hx_1_12-pos_hx_2_12)';
        if numTheta2 ~= length(fieldnames(theta2))
            theta2Test = reduceTheta2(theta2,AVarTheta2.names);
        else
            theta2Test = theta2;
        end
        theta2TestValues = struct2array(theta2Test)';
        Q_slope_12 = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
        pValue_slope_12 = chi2cdf(Q_slope_12,2*2,'upper');
    end
    
    % testing lower left block: hx_21
    pos_hx_1_21 = zeros(numTheta2,2*2);
    pos_hx_2_21 = zeros(numTheta2,2*2);
    idx      = 0;
    for i=3:4
        for j=1:2
            idx = idx + 1;
            pos_hx_1_21(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2_21(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
    end
    if sum(pos_hx_2_21(:)) == 0
        % no switching parameters
        pValue_slope_21 = NaN;
    else
        R = (pos_hx_1_21-pos_hx_2_21)';
        if numTheta2 ~= length(fieldnames(theta2))
            theta2Test = reduceTheta2(theta2,AVarTheta2.names);
        else
            theta2Test = theta2;
        end
        theta2TestValues = struct2array(theta2Test)';
        Q_slope_21 = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
        pValue_slope_21 = chi2cdf(Q_slope_21,2*2,'upper');
    end
    
    % testing lower right block: hx_22
    pos_hx_1_22 = zeros(numTheta2,2*2);
    pos_hx_2_22 = zeros(numTheta2,2*2);
    idx      = 0;
    for i=3:4
        for j=3:4
            idx = idx + 1;
            pos_hx_1_22(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2_22(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
    end
    if sum(pos_hx_2_22(:)) == 0
        % no switching parameters
        pValue_slope_22 = NaN;
    else
        R = (pos_hx_1_22-pos_hx_2_22)';
        if numTheta2 ~= length(fieldnames(theta2))
            theta2Test = reduceTheta2(theta2,AVarTheta2.names);
        else
            theta2Test = theta2;
        end
        theta2TestValues = struct2array(theta2Test)';
        Q_slope_22 = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
        pValue_slope_22 = chi2cdf(Q_slope_22,2*2,'upper');
    end
else
    Q_slope         = NaN;
    pValue_slope    = NaN;
    pValue_slope_11 = NaN;
    pValue_slope_12 = NaN;
    pValue_slope_21 = NaN;
    pValue_slope_22 = NaN;
end

% Joint test for change in intercepts and slope if possible
if any(strcmp('h01_2',AVarTheta2.names)) && any(strcmp('hx11_2',AVarTheta2.names))
    numTheta2 = length(AVarTheta2.names);
    pos_h0_1 = zeros(numTheta2,nx);
    pos_h0_2 = zeros(numTheta2,nx);
    for i=1:nx
        pos_h0_1(:,i) = strcmp(['h0',num2str(i),'_1'],AVarTheta2.names);
        pos_h0_2(:,i) = strcmp(['h0',num2str(i),'_2'],AVarTheta2.names);
    end
    numTheta2 = length(AVarTheta2.names);
    pos_hx_1 = zeros(numTheta2,nx*nx);
    pos_hx_2 = zeros(numTheta2,nx*nx);
    idx      = 0;
    for i=1:nx
        for j=1:nx
            idx = idx + 1;
            pos_hx_1(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
            pos_hx_2(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
        end
    end
    R = [(pos_h0_1-pos_h0_2)';(pos_hx_1-pos_hx_2)'];
    theta2Test = theta2;
    theta2TestValues = struct2array(theta2Test)';
    Q_InterceptSlope = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
    pValue_InterceptSlope = chi2cdf(Q_InterceptSlope,nx+nx*nx,'upper');
else
    Q_InterceptSlope = NaN;
    pValue_InterceptSlope = NaN;
end


% The output
out.Q_intercept      = Q_intercept;
out.pValue_intercept = pValue_intercept;
out.Q_slope          = Q_slope;
out.pValue_slope     = pValue_slope;
out.pValue_slope_11       = pValue_slope_11;
out.pValue_slope_12       = pValue_slope_12;
out.pValue_slope_21       = pValue_slope_21;
out.pValue_slope_22       = pValue_slope_22;
out.Q_InterceptSlope = Q_InterceptSlope;
out.pValue_InterceptSlope = pValue_InterceptSlope;
